var Router = require('koa-router');
var service = require('./service');

var router = module.exports = new Router({
  prefix: '/group'
});

// 群组公告
router.get('/notice/:id', function * (next) {
  var isManager = false;
  var data = yield service.getNotice(this, this.params.id);
  if (this.session.account && this.query.gcRoomId) {
    var role = yield service.roleOfGroup(this, this.session.account.accountId, this.query.gcRoomId);
    isManager = (role.roleType === 2 || role.roleType === 3);
  }

  this.body = this.template.render('templates/notice.html', data, {
    isManager: isManager
  });
});

// 删除公告
router.post('/ajax/notice', loginRequired, function * (next) {
  var self = this;
  var form = this.request.body;
  yield service.delNotice(this, this.session.account.accountId, this.session.member.name, form.id).then(function (data) {
    self.body = self.render(200, data);
  }, function (data) {
    self.body = self.render(500, data);
  });
});